<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>query_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_third_party_closure_goog_dojo_dom_query_test.js.html">query_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>goog.require(&#39;goog.dom&#39;);
<a name="line2"></a>goog.require(&#39;goog.dom.query&#39;);
<a name="line3"></a>goog.require(&#39;goog.testing.asserts&#39;);  // assertThrows
<a name="line4"></a>
<a name="line5"></a>goog.setTestOnly(&#39;query_test&#39;);
<a name="line6"></a>
<a name="line7"></a>function testBasicSelectors() {
<a name="line8"></a>  assertQuery(4, &#39;h3&#39;);
<a name="line9"></a>  assertQuery(1, &#39;h1:first-child&#39;);
<a name="line10"></a>  assertQuery(2, &#39;h3:first-child&#39;);
<a name="line11"></a>  assertQuery(1, &#39;#t&#39;);
<a name="line12"></a>  assertQuery(1, &#39;#bug&#39;);
<a name="line13"></a>  assertQuery(4, &#39;#t h3&#39;);
<a name="line14"></a>  assertQuery(1, &#39;div#t&#39;);
<a name="line15"></a>  assertQuery(4, &#39;div#t h3&#39;);
<a name="line16"></a>  assertQuery(0, &#39;span#t&#39;);
<a name="line17"></a>  assertQuery(1, &#39;#t div &gt; h3&#39;);
<a name="line18"></a>  assertQuery(2, &#39;.foo&#39;);
<a name="line19"></a>  assertQuery(1, &#39;.foo.bar&#39;);
<a name="line20"></a>  assertQuery(2, &#39;.baz&#39;);
<a name="line21"></a>  assertQuery(3, &#39;#t &gt; h3&#39;);
<a name="line22"></a>}
<a name="line23"></a>
<a name="line24"></a>function testSyntacticEquivalents() {
<a name="line25"></a>  // syntactic equivalents
<a name="line26"></a>  assertQuery(12, &#39;#t &gt; *&#39;);
<a name="line27"></a>  assertQuery(12, &#39;#t &gt;&#39;);
<a name="line28"></a>  assertQuery(3, &#39;.foo &gt; *&#39;);
<a name="line29"></a>  assertQuery(3, &#39;.foo &gt;&#39;);
<a name="line30"></a>}
<a name="line31"></a>
<a name="line32"></a>function testWithARootById() {
<a name="line33"></a>  // with a root, by ID
<a name="line34"></a>  assertQuery(3, &#39;&gt; *&#39;, &#39;container&#39;);
<a name="line35"></a>  assertQuery(3, &#39;&gt; h3&#39;, &#39;t&#39;);
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function testCompoundQueries() {
<a name="line39"></a>  // compound queries
<a name="line40"></a>  assertQuery(2, &#39;.foo, .bar&#39;);
<a name="line41"></a>  assertQuery(2, &#39;.foo,.bar&#39;);
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>function testMultipleClassAttributes() {
<a name="line45"></a>  // multiple class attribute
<a name="line46"></a>  assertQuery(1, &#39;.foo.bar&#39;);
<a name="line47"></a>  assertQuery(2, &#39;.foo&#39;);
<a name="line48"></a>  assertQuery(2, &#39;.baz&#39;);
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>function testCaseSensitivity() {
<a name="line52"></a>  // case sensitivity
<a name="line53"></a>  assertQuery(1, &#39;span.baz&#39;);
<a name="line54"></a>  assertQuery(1, &#39;sPaN.baz&#39;);
<a name="line55"></a>  assertQuery(1, &#39;SPAN.baz&#39;);
<a name="line56"></a>  assertQuery(1, &#39;[class = \&quot;foo bar\&quot;]&#39;);
<a name="line57"></a>  assertQuery(2, &#39;[foo~=\&quot;bar\&quot;]&#39;);
<a name="line58"></a>  assertQuery(2, &#39;[ foo ~= \&quot;bar\&quot; ]&#39;);
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function testAttributes() {
<a name="line62"></a>  assertQuery(3, &#39;[foo]&#39;);
<a name="line63"></a>  assertQuery(1, &#39;[foo$=\&quot;thud\&quot;]&#39;);
<a name="line64"></a>  assertQuery(1, &#39;[foo$=thud]&#39;);
<a name="line65"></a>  assertQuery(1, &#39;[foo$=\&quot;thudish\&quot;]&#39;);
<a name="line66"></a>  assertQuery(1, &#39;#t [foo$=thud]&#39;);
<a name="line67"></a>  assertQuery(1, &#39;#t [ title $= thud ]&#39;);
<a name="line68"></a>  assertQuery(0, &#39;#t span[ title $= thud ]&#39;);
<a name="line69"></a>  assertQuery(2, &#39;[foo|=\&quot;bar\&quot;]&#39;);
<a name="line70"></a>  assertQuery(1, &#39;[foo|=\&quot;bar-baz\&quot;]&#39;);
<a name="line71"></a>  assertQuery(0, &#39;[foo|=\&quot;baz\&quot;]&#39;);
<a name="line72"></a>}
<a name="line73"></a>
<a name="line74"></a>function testDescendantSelectors() {
<a name="line75"></a>  assertQuery(3, &#39;&gt;&#39;, &#39;container&#39;);
<a name="line76"></a>  assertQuery(3, &#39;&gt; *&#39;, &#39;container&#39;);
<a name="line77"></a>  assertQuery(2, &#39;&gt; [qux]&#39;, &#39;container&#39;);
<a name="line78"></a>  assertEquals(&#39;child1&#39;, goog.dom.query(&#39;&gt; [qux]&#39;, &#39;container&#39;)[0].id);
<a name="line79"></a>  assertEquals(&#39;child3&#39;, goog.dom.query(&#39;&gt; [qux]&#39;, &#39;container&#39;)[1].id);
<a name="line80"></a>  assertQuery(3, &#39;&gt;&#39;, &#39;container&#39;);
<a name="line81"></a>  assertQuery(3, &#39;&gt; *&#39;, &#39;container&#39;);
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>function testSiblingSelectors() {
<a name="line85"></a>  assertQuery(1, &#39;+&#39;, &#39;container&#39;);
<a name="line86"></a>  assertQuery(3, &#39;~&#39;, &#39;container&#39;);
<a name="line87"></a>  assertQuery(1, &#39;.foo + span&#39;);
<a name="line88"></a>  assertQuery(4, &#39;.foo ~ span&#39;);
<a name="line89"></a>  assertQuery(1, &#39;#foo ~ *&#39;);
<a name="line90"></a>  assertQuery(1, &#39;#foo ~&#39;);
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>function testSubSelectors() {
<a name="line94"></a>  // sub-selector parsing
<a name="line95"></a>  assertQuery(1, &#39;#t span.foo:not(span:first-child)&#39;);
<a name="line96"></a>  assertQuery(1, &#39;#t span.foo:not(:first-child)&#39;);
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>function testNthChild() {
<a name="line100"></a>  assertEquals(goog.dom.$(&#39;_foo&#39;), goog.dom.query(&#39;.foo:nth-child(2)&#39;)[0]);
<a name="line101"></a>  assertQuery(2, &#39;#t &gt; h3:nth-child(odd)&#39;);
<a name="line102"></a>  assertQuery(3, &#39;#t h3:nth-child(odd)&#39;);
<a name="line103"></a>  assertQuery(3, &#39;#t h3:nth-child(2n+1)&#39;);
<a name="line104"></a>  assertQuery(1, &#39;#t h3:nth-child(even)&#39;);
<a name="line105"></a>  assertQuery(1, &#39;#t h3:nth-child(2n)&#39;);
<a name="line106"></a>  assertQuery(1, &#39;#t h3:nth-child(2n+3)&#39;);
<a name="line107"></a>  assertQuery(2, &#39;#t h3:nth-child(1)&#39;);
<a name="line108"></a>  assertQuery(1, &#39;#t &gt; h3:nth-child(1)&#39;);
<a name="line109"></a>  assertQuery(3, &#39;#t :nth-child(3)&#39;);
<a name="line110"></a>  assertQuery(0, &#39;#t &gt; div:nth-child(1)&#39;);
<a name="line111"></a>  assertQuery(7, &#39;#t span&#39;);
<a name="line112"></a>  assertQuery(3, &#39;#t &gt; *:nth-child(n+10)&#39;);
<a name="line113"></a>  assertQuery(1, &#39;#t &gt; *:nth-child(n+12)&#39;);
<a name="line114"></a>  assertQuery(10, &#39;#t &gt; *:nth-child(-n+10)&#39;);
<a name="line115"></a>  assertQuery(5, &#39;#t &gt; *:nth-child(-2n+10)&#39;);
<a name="line116"></a>  assertQuery(6, &#39;#t &gt; *:nth-child(2n+2)&#39;);
<a name="line117"></a>  assertQuery(5, &#39;#t &gt; *:nth-child(2n+4)&#39;);
<a name="line118"></a>  assertQuery(5, &#39;#t &gt; *:nth-child(2n+4)&#39;);
<a name="line119"></a>  assertQuery(12, &#39;#t &gt; *:nth-child(n-5)&#39;);
<a name="line120"></a>  assertQuery(6, &#39;#t &gt; *:nth-child(2n-5)&#39;);
<a name="line121"></a>}
<a name="line122"></a>
<a name="line123"></a>function testEmptyPseudoSelector() {
<a name="line124"></a>  assertQuery(4, &#39;#t &gt; span:empty&#39;);
<a name="line125"></a>  assertQuery(6, &#39;#t span:empty&#39;);
<a name="line126"></a>  assertQuery(0, &#39;h3 span:empty&#39;);
<a name="line127"></a>  assertQuery(1, &#39;h3 :not(:empty)&#39;);
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>function testIdsWithColons(){
<a name="line131"></a>  assertQuery(1, &quot;#silly\\:id\\:\\:with\\:colons&quot;);
<a name="line132"></a>}
<a name="line133"></a>
<a name="line134"></a>function testOrder() {
<a name="line135"></a>  var els = goog.dom.query(&#39;.myupperclass .myclass input&#39;);
<a name="line136"></a>  assertEquals(&#39;myid1&#39;, els[0].id);
<a name="line137"></a>  assertEquals(&#39;myid2&#39;, els[1].id);
<a name="line138"></a>}
<a name="line139"></a>
<a name="line140"></a>function testCorrectDocumentInFrame() {
<a name="line141"></a>  var frameDocument = window.frames[&#39;ifr&#39;].document;
<a name="line142"></a>  frameDocument.body.innerHTML =
<a name="line143"></a>      document.getElementById(&#39;iframe-test&#39;).innerHTML;
<a name="line144"></a>
<a name="line145"></a>  var els = goog.dom.query(&#39;#if1 .if2 div&#39;, document);
<a name="line146"></a>  var frameEls = goog.dom.query(&#39;#if1 .if2 div&#39;, frameDocument);
<a name="line147"></a>
<a name="line148"></a>  assertEquals(els.length, frameEls.length);
<a name="line149"></a>  assertEquals(1, frameEls.length);
<a name="line150"></a>  assertNotEquals(document.getElementById(&#39;if3&#39;),
<a name="line151"></a>                  frameDocument.getElementById(&#39;if3&#39;));
<a name="line152"></a>}
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * @param {number} expectedNumberOfNodes
<a name="line156"></a> * @param {...*} var_args
<a name="line157"></a> */
<a name="line158"></a>function assertQuery(expectedNumberOfNodes, var_args) {
<a name="line159"></a>  var args = Array.prototype.slice.call(arguments, 1);
<a name="line160"></a>  assertEquals(expectedNumberOfNodes,
<a name="line161"></a>               goog.dom.query.apply(null, args).length);
<a name="line162"></a>}
</pre>


</body>
</html>
